###############################################################################
# Copyright (C) 2010 Chris Wroe
#
# Modifications to the initial code base are copyright of their respective
# authors, or their employers as appropriate.
#
# This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU Lesser General Public License as published by the Free
# Software Foundation; either version 2.1 of the License, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
# details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program; if not, write to the Free Software Foundation, Inc.,
# 59 Temple Place, Suite 330, Boston, MA 02111-1307
##############################################################################

require "rubygems"
require "neo4j"
require 'lucene'
require "model"

require "load_snomed_component"

include Lucene

class LoadSnomedRelationshipComponent < LoadSnomedComponent

  def initialize(format_file)
    @field_mapper = FieldToPropertyMap.new
    @field_mapper.load_config(format_file)

    
    super()
  end

  

  def create_component(line,inserter,global_properties)
    raise if !line or line.strip.empty?
    @field_mapper.data=line

    current_properties = {}.merge! global_properties
    current_properties.merge! @field_mapper.property_map

    source_node=@field_mapper.get_relationship_node('concept_id_1', global_properties)
    target_node=@field_mapper.get_relationship_node('concept_id_2', global_properties)
#    puts "relationship type  #{current_properties['relationship_type']} characteristic type #{current_properties['characteristic_type']}"
    case current_properties['characteristic_type']
    when 0 # defining
      if current_properties['relationship_type']==116680003
        rel_name = 'Concept#isa_relations'
      else
        rel_name = 'Concept#intrinsic_relations'
      end
    when 1
        rel_name = 'Concept#qualifier_relations'
    when 2
      rel_name = 'Concept#historical_relations'
    when 3
      rel_name = 'Concept#extrinsic_relations'
    end
    @java_concept_rel_type = org.neo4j.graphdb.DynamicRelationshipType.withName(rel_name)

#    current_properties.each do |k,v|
#      puts "#{k} #{v} #{v.class}"
#    end

    #    puts "#{source_node} #{@java_concept_rel_type} #{target_node} #{current_properties}"

    inserter.createRelationship(source_node,target_node,@java_concept_rel_type,current_properties)

    #    raise if tab_items[0].empty?
    #    concept_id_1 = tab_items.shift.strip
    #    concept_node_ids = find_concept_node_ids(concept_id_1, @concept_index)
    #    raise if not concept_node_ids.size==1
    #    concept_node_id = concept_node_ids[0]
    #
    #    raise if tab_items[0].empty?
    #    relationship_type_id = tab_items.shift.strip
    #    relationship_type_node_ids = find_concept_node_ids(relationship_type_id, @concept_index)
    #    raise if not relationship_type_node_ids.size==1
    #    relationship_type_node_id=relationship_type_node_ids[0]







  end

  def batch_insert_from_file(f,global_properties)
    component_inserted_count =0

    #@relationship_index = Index.new('/Description')
    #@relationship_index.field_infos[:term][:tokenized] = true
    Neo4j::BatchInserter.new do |b|

      #skip header line
      read_row(f)

      while !f.eof?
        line = read_row(f)
        create_component(line,b,global_properties)
        component_inserted_count+=1
        if component_inserted_count % 25000 == 0
          #@description_index.commit
          puts component_inserted_count
        end
      end
      #@description_index.commit

    end
    return component_inserted_count
  end


end
